package com.wc.算法提高课.B第二章_搜索.双向广搜.字串变换;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/9/25 16:43
 * @description https://www.acwing.com/problem/content/192/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 7;
    static String[] a = new String[N], b = new String[N];
    static Map<String, Integer> da = new HashMap<>(), db = new HashMap<>();
    static Queue<String> qa = new LinkedList<>(), qb = new LinkedList<>();
    static String A, B;
    static int n = 0;

    public static void main(String[] args) {
        A = sc.next();
        B = sc.next();

        while (sc.hasNext()) {
            a[n] = sc.next();
            if (a[n].equals("#")) break;
            b[n] = sc.next();
            n++;
        }

        int res = bfs();
        if (res > 10) out.println("NO ANSWER!");
        else out.println(res);
        out.flush();
    }

    static int bfs() {
        if (A.equals(B)) return 0;
        qa.add(A);
        qb.add(B);
        da.put(A, 0);
        db.put(B, 0);
        while (!qa.isEmpty() && !qb.isEmpty()) {
            int step;
            if (qa.size() <= qb.size()) step = expend(qa, qb, da, db, a, b);
            else step = expend(qb, qa, db, da, b, a);
            if (step <= 10) return step;
        }

        return 11;
    }

    static int expend(Queue<String> qa, Queue<String> qb, Map<String, Integer> da, Map<String, Integer> db, String[] a, String[] b) {
        String t = qa.poll();
        assert t != null;
        int len = t.length();
        int dist = da.get(t);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < len; j++) {
                if (t.indexOf(a[i], j) == j) {
                    String state = t.substring(0, j) + b[i] + t.substring(j + a[i].length());
                    if (db.containsKey(state)) return dist + 1 + db.get(state);
                    if (da.containsKey(state)) continue;
                    da.put(state, dist + 1);
                    qa.add(state);
                }
            }
        }
        return 11;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
